//
//  DBYDestinationViewController.m
//  MemoryTravel
//
//  Created by dllo on 15/12/28.
//  Copyright © 2015年 fcx. All rights reserved.
//

#import "DBYDestinationViewController.h"

@interface DBYDestinationViewController ()<UITableViewDataSource,UITableViewDelegate>

@property(nonatomic,strong)UITableView *myTableView;

@property (nonatomic, retain) NSMutableArray *array;

@property (nonatomic, assign) BOOL isUpLoad;

@property (nonatomic, assign) NSInteger page;

@end

@implementation DBYDestinationViewController

- (void)viewDidLoad {

    [super viewDidLoad];

    self.view.backgroundColor = [UIColor whiteColor];

    self.navigationItem.title = [NSString stringWithFormat:@"%@旅行地", self.roadModel.name];
    
    self.myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, Width, Height - 66 ) style:UITableViewStylePlain];
    [self.view addSubview:self.myTableView];
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;

    self.array = [NSMutableArray array];

    [self.myTableView registerClass:[DBYDestinationCell class] forCellReuseIdentifier:@"DBYDestinationCell"];

    [self getAFNetWorkingData];
    [self addHeaderRefresh];
    [self addFooterRefresh];

    // Do any additional setup after loading the view.
}

- (void)addHeaderRefresh{
    
    self.myTableView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        self.isUpLoad = NO;
        if (self == 0) {
            self.page = 1;
        }
        // 重新请求数据
        [self getAFNetWorkingData];
    }];
    

    [self.myTableView.header beginRefreshing];
}

#pragma mark -- 设置上拉刷新
-(void)addFooterRefresh{
  self.myTableView.footer = [MJRefreshAutoFooter footerWithRefreshingBlock:^{

      self.isUpLoad = YES;
      self.page++;

      // 重新请求数据
      [self getAFNetWorkingData];
  }];
}

- (void)getAFNetWorkingData{
    if (self.isUpLoad == NO) {
        
        NSString *url_string = [NSString stringWithFormat:@"http://chanyouji.com/api/destinations/attractions/%@.json?per_page=20&page=1",self.roadModel.nameId];
        [AppTools getDataWithUrlStr:url_string andBlock:^(id result) {

            [self.array removeAllObjects];

            for (NSMutableDictionary *dic in result) {

                DBYDestinationModel *destinaModel = [[DBYDestinationModel alloc] initWithDictionary:dic];

                [self.array addObject:destinaModel];

            }
            [self.myTableView reloadData];
            
        }];
        
    }else{
        
        NSString *url_string = [NSString stringWithFormat:@"http://chanyouji.com/api/destinations/attractions/%@.json?per_page=20&page=%ld",self.roadModel.nameId, self.page];
        
        [AppTools getDataWithUrlStr:url_string andBlock:^(id result) {

            for (NSMutableDictionary *dic in result) {
                DBYDestinationModel *destinaModel = [[DBYDestinationModel alloc] initWithDictionary:dic];
                [self.array addObject:destinaModel];
                
            }
            
        }];
        
    }

    [self.myTableView.header endRefreshing];
    [self.myTableView.footer endRefreshing];
    [self.myTableView reloadData];
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.array.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    DBYDestinationCell *cell = [tableView dequeueReusableCellWithIdentifier:@"DBYDestinationCell"];

    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    DBYDestinationModel *destinaModel = [self.array objectAtIndex:indexPath.row];

    cell.aModel = destinaModel;
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 100 ;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    DBYDestinaDetailViewController *DestinationDetailVC = [[DBYDestinaDetailViewController alloc] init];
    DBYDestinationModel *destinationModel = [self.array objectAtIndex:indexPath.row];
    DestinationDetailVC.aModel = destinationModel;
    
    
    
    [self.navigationController pushViewController:DestinationDetailVC animated:YES];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
